package com.qiangesoft.bootcodegen.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qiangesoft.bootcodegen.entity.BcgColumnMap;
import com.qiangesoft.bootcodegen.mapper.BcgColumnMapMapper;
import com.qiangesoft.bootcodegen.pojo.convert.BcgColumnMapConvert;
import com.qiangesoft.bootcodegen.pojo.dto.BcgColumnMapDTO;
import com.qiangesoft.bootcodegen.pojo.query.BcgColumnMapQuery;
import com.qiangesoft.bootcodegen.pojo.query.PageQuery;
import com.qiangesoft.bootcodegen.pojo.vo.PageResultVO;
import com.qiangesoft.bootcodegen.service.IBcgColumnMapService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 字段类型映射 服务实现类
 * </p>
 *
 * @author qiangesoft
 * @date 2024-04-01
 */
@Service
public class BcgColumnMapServiceImpl extends ServiceImpl<BcgColumnMapMapper, BcgColumnMap> implements IBcgColumnMapService {

    @Override
    public void saveColumnMap(BcgColumnMapDTO columnMapDTO) {
        BcgColumnMap bcgColumnMap = BcgColumnMapConvert.INSTANCE.dtoToEntity(columnMapDTO);
        baseMapper.insert(bcgColumnMap);
    }

    @Override
    public void removeColumnMap(List<Long> ids) {
        baseMapper.deleteBatchIds(ids);
    }

    @Override
    public void updateColumnMap(Long id, BcgColumnMapDTO columnMapDTO) {
        BcgColumnMap bcgColumnMap = BcgColumnMapConvert.INSTANCE.dtoToEntity(columnMapDTO);
        bcgColumnMap.setId(id);
        baseMapper.updateById(bcgColumnMap);
    }

    @Override
    public PageResultVO<BcgColumnMap> listColumnMap(PageQuery pageQuery, BcgColumnMapQuery columnMapQuery) {
        long pageNum = pageQuery.getPageNum();
        long pageSize = pageQuery.getPageSize();

        PageResultVO<BcgColumnMap> pageResult = new PageResultVO<>();
        pageResult.setPageNum(pageNum);
        pageResult.setPageSize(pageSize);

        LambdaQueryWrapper<BcgColumnMap> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(columnMapQuery.getDbType()), BcgColumnMap::getDbType, columnMapQuery.getDbType())
                .like(StringUtils.isNotBlank(columnMapQuery.getColumnType()), BcgColumnMap::getColumnType, columnMapQuery.getColumnType())
                .orderByDesc(BcgColumnMap::getCreateTime);
        Page<BcgColumnMap> page = baseMapper.selectPage(new Page<>(pageNum, pageSize), queryWrapper);
        pageResult.setTotal(page.getTotal());
        pageResult.setPages(page.getPages());
        pageResult.setResults(page.getRecords());

        return pageResult;
    }

    @Override
    public List<BcgColumnMap> listColumnMapByDbType(String dbType) {
        LambdaQueryWrapper<BcgColumnMap> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BcgColumnMap::getDbType, dbType);
        return baseMapper.selectList(queryWrapper);
    }
}
